
package com.wobaby.util.config;

import java.util.*;

// Referenced classes of package com.wobaby.util.config:
//            Config, ConfigOptionsDiff, ConfigRetriever, OptionHistory

public class LocalConfigOptions
{

    public LocalConfigOptions()
    {
    }

    public void merge(ConfigOptionsDiff incomingOptions)
    {
        synchronized(Config.getConfig())
        {
            Map changedOptionMap = new HashMap();
            Iterator it = incomingOptions.getTransferableChanges();
            do
            {
                if(!it.hasNext())
                    break;
                String optionName = (String)it.next();
                Date incomingOptionChangedDate = incomingOptions.getOptionChangedDate(optionName);
                Date localOptionChangedDate = getLocalOptionChangedDate(optionName);
                if(localOptionChangedDate == null || incomingOptionChangedDate.after(localOptionChangedDate))
                    changedOptionMap.put(optionName, incomingOptions.getOptionHistory(optionName));
            } while(true);
            writeOptionValues(changedOptionMap);
        }
        Config.getConfig().reload(false);
    }

    private Date getLocalOptionChangedDate(String optionName)
    {
        Date localOptionChangedDate = null;
        OptionHistory oh = Config.getConfig().getOptionHistory(optionName);
        if(oh != null)
            localOptionChangedDate = oh.lastModifiedDate;
        return localOptionChangedDate;
    }

    private void writeOptionValues(Map changedOptionMap)
    {
        Config.getConfig().updateLocal(changedOptionMap);
    }
}